<?php
namespace app\admin\controller;
class NewController extends Basic
{
    public function index(){
        $this->redirect(url('index/index'));
    }
    
    public function news(){
        $this->menu(49,50);
        $model = \think\Db::table('news')->where(1)->select();
        $this->assign('list',$model);
        return $this->fetch();
    }
    
    public function updatenews(){
        $this->menu(49,50);
        $id = input('id');
        
        $model = \think\Db::table('news')->where('id',$id)->find();
        $this->assign('data',$model);
        
        return $this->fetch();
    }

    public function addNews(){
        $this->menu(49,51);
        return $this->fetch();
    }
    
    public function delNews(){
        $id = input('id');
        $ret = \think\Db::table('news')->where('id',$id)->delete();
        
        if($ret){
            $this->success('成功','new_controller/news');
        }else{
            $this->error('错误');
        }
    }
    
    public function savenews(){
       if(isset($_POST['delPic'])){
            $delPhoto = $_POST['delPic'];
            foreach($delPhoto as $v){
                if(strlen($v)>1){
                    $this->delPhoto($v);
                }
            }
        } 
        
        
       $id = input('id');
       $author = input('author','','trim');
       $title = input('title');
       $mainPic=input('mainPic');
       $content = input('content');
       
       $arr = [
           'table'=>'news',
           'id'=>$id,
           'author'=>$author,
           'mainPic'=>$mainPic,
           'title'=>$title,
           'content'=>$content,
           'time'=>date('Y-m-d H:i:s'),
           'dateT'=>date('Y-m-d'),
       ];
       
         $crud = \think\Loader::model('Crud','logic');
         $ret = $crud->SaveData($arr);
         
         if($ret){
             return $this->success('成功','new_controller/news');
         }  else {
             return $this->error('错误');
         }
    }
    
    public function savePic(){
        $typeArr = array("jpg", "png", "gif");//允许上传文件格式 
        $path= ROOT_PATH.'public\uploads\news';

        if (isset($_POST)) { 
        $name = $_FILES['file']['name']; 
        $size = $_FILES['file']['size']; 
        $name_tmp = $_FILES['file']['tmp_name']; 
        if (empty($name)) { 
            echo json_encode(array("error"=>"您还未选择图片")); 
            exit; 
        } 
        $type = strtolower(substr(strrchr($name, '.'), 1)); //获取文件类型 

        if (!in_array($type, $typeArr)) { 
            echo json_encode(array("error"=>"清上传jpg,png或gif类型的图片！")); 
            exit; 
        } 
        if ($size > (3072 * 1024)) { 
            echo json_encode(array("error"=>"图片大小已超过3m！")); 
            exit; 
        } 

        $pic_name = time() . rand(10000, 99999) . "." . $type;//图片名称 
        $pic_url ="$path\\$pic_name";//上传后图片路径+名称 
        if (move_uploaded_file($name_tmp, $pic_url)) { //临时文件转移到目标文件夹 
            return json_encode(array("error"=>"0","pic"=>"/uploads/news/$pic_name","name"=>$pic_name)); 
        } else { 
            return json_encode(array("error"=>"上传有误，清检查服务器配置！")); 
        } 
        }
    }
}